var searchEnergyDetail;
//跳转到线路信息页面
var goLineInfo;
//跳转到车辆信息页面
var goCarInfo;
require(['grid', 'topBar', 'layout', 'date'], function () {
    var postData = null;
    	postData = {};
    var $gridList = $('#grid'),
        $top = $('#top');
    $('body').layout();
    $top.topBar({
        left: [{
            type: 'filter-more',
            dateType: 'custom',
            search: function () {
                $gridList.grid('setGridParam', {
                    url: $.getProjectName() + "/carEnergy/searchCarEnergy.htm"
                });
                if (getPostData()) {
                    $gridList.grid('setGridParam', {
                        datatype: "json",
                        postData: postData
                    }).trigger("reloadGrid");
                }
            },
            item: [
                {
                    text: '时间快捷',
                    name1: 'beginTime',
                    name2: 'endTime',
                    type: 'dateLine'
                }, {
                    text: '按条件汇总',
                    name: 'energyTimeType',
                    type: 'item-select',
                    data: [
                        {text: '按日汇总', value: '10'},
                        {text: '按月汇总', value: '7'},
                        {text: '总体汇总', value: ''}
                    ]
                }, {
                    text: '燃料类型',
                    name: 'fuelTypeCode',
                    type: 'item-select',
                    data: [
                        {text: '全部', value: ''},
                        {text: '柴油', value: '1'},
                        {text: '电能', value: '3'},
                        {text: 'CNG', value: '4'},
                        {text: 'LNG', value: '5'},
                        {text: '汽油', value: '2'}
                    ]
                }
            ]
        }],
        right: [
            {
                text: '导出',
                type: 'export-btn',
                authority: '',
                item: [
                    {
                        text: 'Excel导出',
                        click: function () {
                            exportFile('Excel');
                        }
                    },
                    {
                        text: 'PDF导出',
                        click: function () {
                            exportFile('Pdf');
                        }
                    }
                ]
            }
        ]
    });

    function exportFile(suffix) {
        if ($gridList.getGridParam("reccount") > 0 && getPostData()) {
            //var $postData=$("#top").parent().getValues();
            var url = "/carEnergy/exportCarEnergy" + suffix + ".htm";
            $.exportFile(url, postData);
        } else {
            $.messager.alert("警告", "请先查询数据！", "info");
        }
    }

    $gridList.grid({
        url: "/carEnergy/searchCarEnergy.htm",
        sortname: "DAYS,CAR_MODEL,CAR_ENER_CONS,CAR_ID,FUEL_TYPE_CODE",
        shrinkToFit: false,
        //tableId:'8ec82ba949f84a048940d0e18be20ce7',
        colModel: [
            {name: "id", label: 'id', key: true, hidden: true},
            {name: "carId", label: 'carId', key: true, hidden: true},
            {name: "lineId", label: 'lineId', hidden: true},
            {name: "fuelTypeCode", label: 'fuelTypeCode', hidden: true},
            {
                name: "allrank", label: '能耗排名', align: "center", width:80,
                cellattr: function (rowId) {
                    return 'id=\'allrank' + rowId + "\'";
                }
            },
            {
                name: "organName", label: '所属机构', align: "center", width:80,
                cellattr: function (rowId) {
                    return 'id=\'organName' + rowId + "\'";
                }
            },
            {
                name: "lineName", label: '营运线路', align: "center",width:100,formatter: 'customCurrency',formatoptions:{
	                redbtn:true,
	                css:'text-link c-blue',
	                clickFn:function(rowObject,cellvalue){
	                	goLineInfo(rowObject.lineId);
	                }
	            }
            },
            {
                name: "licPltNo", label: '车牌号', align: "center", width:80,formatter:  function (cellvalue,options,rowObject) {
                	return "<b><a href='javascript:void(0);' onclick='goCarInfo(\"" + rowObject.carId + "\")' class='text-link c-blue'>" + cellvalue + "</a></b>";
                }
            },
            {
                name: "carInCd", label: '车辆编号', align: "center", width:80,
                cellattr: function (rowId) {
                    return 'id=\'carInCd' + rowId + "\'";
                }
            },
            {
                name: "carType", label: '车辆类型', align: "center", width:120,
                cellattr: function (rowId) {
                    return 'id=\'carType' + rowId + "\'";
                }
            },
            {
                name: "carModel", label: '车辆型号', align: "center", width:120,
                cellattr: function (rowId) {
                    return 'id=\'carModel' + rowId + "\'";
                }
            },
            {
                name: "fuelTypeName", label: '燃料类型', align: "center", width:80,
                cellattr: function (rowId) {
                    return 'id=\'fuelTypeName' + rowId + "\'";
                }
            },
//	               {name: "engiNo", label: '电机型号', align: "center",
//	                   cellattr: function(rowId, tv, rawObject, cm, rdata) {
//	                        return 'id=\'engiNo' + rowId + "\'";
//	                    }},
            {
                name: "days", label: '运营日期', align: "center", width:80,
                cellattr: function (rowId) {
                    return 'id=\'days' + rowId + "\'";
                }
            },
            {
                name: "rngOprtDuraTr", label: '运行时长', align: "right", width:80,
                cellattr: function (rowId) {
                    return 'id=\'rngOprtDuraTr' + rowId + "\'";
                }
            },
            {
                name: "totalMile", label: '总里程', formatter: "currency", align: "right", formatter: "currency", width:80,
                formatter: function (cellvalue) {
                    return "" + (cellvalue||'');
                },
                cellattr: function (rowId) {
                    return 'id=\'totalMile' + rowId + "\'";
                }
            },
            {
                name: "rngMile", label: '运行里程', formatter: "currency", align: "right", formatter: "currency", width:80,
                formatter: function (cellvalue) {
                    return "" + cellvalue;
                },
                cellattr: function (rowId) {
                    return 'id=\'rngMile' + rowId + "\'";
                }
            },
            {name: "rngEnerCons", label: '运行能耗', formatter: "currency", align: "right", width:80,},
            {name: "enerConsMile", label: '百公里能耗', formatter: "currency", align: "right", width:80,},
            {name: "refrEnerCons", label: '标准能耗', formatter: "currency", width: 80, hidden: true},
            {
                name: "enerConsPers", label: '能耗百分比', formatter: "currency", align: "right",width:80,
                formatter: function (cellvalue) {
                    return cellvalue;
                }
            },
            {name: "lineEnerConsMile", label: '线路平均能耗', formatter: "currency", align: "right", width:100},
            {name: "isSaveEner", label: '是否节能', align: "center", width:80}
        ],
        gridComplete: function () {
            Merger('allrank');
//            Merger('licPltNo');
            Merger('carInCd');
//            Merger('lineName');
            Merger('organName');
            Merger('carType');
            Merger('carModel');
            Merger('fuelTypeName');
            Merger('engiNo');
            Merger('days');
            Merger('rngOprtDuraTr');
            Merger('rngMile');
            Merger('totalMile');
        }
    });

    //获取需要传递到后台的参数
    function getPostData() {
        postData = null;
        var type = 'CAR';
        var carIds = commonFun.getLeftTreeCheckedNodes(type);
        if (carIds == null || !carIds.length) {
            $.messager.alert("提示", "请先选择左侧树的车辆!", "warning");
            return false;
        }
        postData = $('#top').parent().getValues();
        postData.orgIds = carIds.toString();
        postData.type = type;
        postData.energyType = "";
        return true;
    }

    searchEnergyDetail = function (data) {
        $("a[data-val]", $top.parent()).removeClass("active");
        $("a[data-val='" + data.dateType + "']", $top.parent()).addClass("active");
        $("a[data-val='" + data.energyTimeType + "']", $top.parent()).addClass("active");
        $("a[data-val='" + data.energyType + "']", $top.parent()).addClass("active");

        postData = $top.parent().getValues();
        postData.orgIds = data.lineId;
        commonFun.setCheckedNodes(data.lineId);
        if(data.lineIds){
        	postData.orgIds = data.lineIds;
        	commonFun.setCheckedNodes(data.lineIds);
        }
        
        postData.type = 'LINE';
        postData.energyType = data.energyType;
        postData.energyTimeType = data.energyTimeType;
        postData.dateType = data.dateType;

        $gridList.grid('setGridParam', {
            datatype: "json",
            postData: postData
        }).trigger("reloadGrid");
    };
    
    
    //跳转到车辆信息页面
    goCarInfo = function(carId){
    	var data = {};
    	data.carId = carId;
    	$.openTab('6bad880fef164ec6adaea7b18420eb6e','showCarInfo',[data]);
    }
    //跳转到线路信息页面
    goLineInfo = function(lineId){
    	var data = {};
    	data.lineId = lineId;
    	$.openTab('4db17f8a322b4749a61eb2f8a12023ae','showLineInfo',[data]);
    }

    //公共调用方法
    function Merger(CellName) {
        //得到显示到界面的id集合
        var mya = $gridList.getDataIDs();
        //当前显示多少条
        var length = mya.length;
        for (var i = 0; i < length; i++) {
            //从上到下获取一条信息
            var before = $gridList.jqGrid('getRowData', mya[i]);
            //如果不是混合动力就跳过
            if (before['carType'] != '混合动力车(新能源)') {
                rowSpanTaxCount = 1;
                continue;
            }
            //定义合并行数
            var rowSpanTaxCount = 1;
            for (var j = i + 1; j <= length; j++) {
                //和上边的信息对比 如果值一样就合并行数+1 然后设置rowspan 让当前单元格隐藏
                var end = $gridList.jqGrid('getRowData', mya[j]);

                if (before[CellName] == end[CellName] && before['licPltNo'] == end['licPltNo']) {
                    rowSpanTaxCount++;
                    $gridList.setCell(mya[j], CellName, '', {display: 'none'});
                } else {
                    rowSpanTaxCount = 1;
                    break;
                }
                $("#" + CellName + "" + mya[i] + "").attr("rowspan", rowSpanTaxCount);
            }
        }
    }
});
